/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: Universidad de Almeria
 * License Type: Purchased
 */
package foroindaltronia.bd;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;

public class TemaDAO {
	public static Tema loadTemaByORMID(int ID) throws PersistentException  {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return loadTemaByORMID(session, ID);
	}
	
	public static Tema loadTemaByORMID(PersistentSession session, int ID) throws PersistentException {
		return (Tema) session.load(foroindaltronia.bd.Tema.class, new Integer(ID));
	}
	
	public static Tema[] listTemaByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return listTemaByQuery(session, condition, orderBy);
	}
	
	public static Tema[] listTemaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From foroindaltronia.bd.Tema as Tema");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			List list = query.list();
			return (Tema[]) list.toArray(new Tema[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Tema loadTemaByQuery(String condition, String orderBy) throws PersistentException {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return loadTemaByQuery(session, condition, orderBy);
	}
	
	public static Tema loadTemaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Tema[] temas = listTemaByQuery(session, condition, orderBy);
		if (temas != null && temas.length > 0)
			return temas[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateTemaByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession();
		return iterateTemaByQuery(session, condition, orderBy);
	}
	
	public static java.util.Iterator iterateTemaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From foroindaltronia.bd.Tema as Tema");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Tema createTema() {
		return new foroindaltronia.bd.Tema();
	}
	
	public static boolean save(foroindaltronia.bd.Tema tema) {
		try {
			foroindaltronia.bd.ProyectoForoPersistentManager.instance().saveObject(tema);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean delete(foroindaltronia.bd.Tema tema) {
		try {
			foroindaltronia.bd.ProyectoForoPersistentManager.instance().deleteObject(tema);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteAndDissociate(foroindaltronia.bd.Tema tema) {
		try {
			if(tema.getGestionado() != null) {
				tema.getGestionado().gestiona.remove(tema);
			}
			
			if(tema.getCreado_por() != null) {
				tema.getCreado_por().crea.remove(tema);
			}
			
			return delete(tema);
		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean deleteAndDissociate(foroindaltronia.bd.Tema tema, org.orm.PersistentSession session) {
		try {
			if(tema.getGestionado() != null) {
				tema.getGestionado().gestiona.remove(tema);
			}
			
			if(tema.getCreado_por() != null) {
				tema.getCreado_por().crea.remove(tema);
			}
			
			try {
				session.delete(tema);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean refresh(foroindaltronia.bd.Tema tema) {
		try {
			foroindaltronia.bd.ProyectoForoPersistentManager.instance().getSession().refresh(tema);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static Tema loadTemaByCriteria(TemaCriteria temaCriteria) {
		Tema[] temas = listTemaByCriteria(temaCriteria);
		if(temas == null || temas.length == 0) {
			return null;
		}
		return temas[0];
	}
	
	public static Tema[] listTemaByCriteria(TemaCriteria temaCriteria) {
		return temaCriteria.listTema();
	}
}
